package com.stylefeng.guns.modular.finance.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.modular.channel.service.IChannelService;
import com.stylefeng.guns.modular.channel.service.IChannelSuperService;
import com.stylefeng.guns.modular.finance.service.ICostChannelSuperService;
import com.stylefeng.guns.modular.system.model.ChannelSuper;
import com.stylefeng.guns.modular.system.model.CostChannelSuper;
import com.stylefeng.guns.modular.system.model.CostService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.CostChannel;
import com.stylefeng.guns.modular.finance.service.ICostChannelService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 渠道推广费控制器
 *
 * @author fengshuonan
 * @Date 2018-08-26 15:55:16
 */
@Controller
@RequestMapping("/costChannel")
public class CostChannelController extends BaseController {

    private String PREFIX = "/finance/costChannel/";

    @Autowired
    private ICostChannelService costChannelService;

    @Autowired
    private ICostChannelSuperService costChannelSuperService;

    @Autowired
    private IChannelSuperService channelSuperService;

    @Autowired
    private IChannelService channelService;

    /**
     * 跳转到渠道推广费首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "costChannel.html";
    }

    /**
     * 跳转到添加渠道推广费
     */
    @RequestMapping("/costChannel_add")
    public String costChannelAdd() {
        return PREFIX + "costChannel_add.html";
    }

    /**
     * 跳转到修改渠道推广费
     */
    @RequestMapping("/costChannel_update/{costChannelId}")
    public String costChannelUpdate(@PathVariable Integer costChannelId, Model model) {
        CostChannel costChannel = costChannelService.selectById(costChannelId);
        model.addAttribute("item",costChannel);
        LogObjectHolder.me().set(costChannel);
        return PREFIX + "costChannel_edit.html";
    }

    /**
     * 获取渠道推广费列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        EntityWrapper<CostChannelSuper> costServiceEntityWrapper = new EntityWrapper<>();
        costServiceEntityWrapper.like("company", condition);
        List<Map<String, Object>> mapList = costChannelSuperService.selectMaps(costServiceEntityWrapper);
//                costChannelService.selectMaps(costServiceEntityWrapper);
        BigDecimal total = new BigDecimal(0.00);
        for (Map<String, Object> map: mapList) {
            total = total.add((BigDecimal) map.get("amount"));
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("amount", total);
        mapList.add(0, map);
        return mapList;
    }

    /**
     * 新增渠道推广费
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CostChannelSuper costChannel) {
        costChannel.setCreateTime(new Date());
        costChannelSuperService.insert(costChannel);
//        costChannelService.insert(costChannel);
        return SUCCESS_TIP;
    }

    /**
     * 删除渠道推广费
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer costChannelId) {
        costChannelService.deleteById(costChannelId);
        return SUCCESS_TIP;
    }

    /**
     * 修改渠道推广费
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(CostChannel costChannel) {
        costChannelService.updateById(costChannel);
        return SUCCESS_TIP;
    }

    /**
     * 渠道推广费详情
     */
    @RequestMapping(value = "/detail/{costChannelId}")
    @ResponseBody
    public Object detail(@PathVariable("costChannelId") Integer costChannelId) {
        return costChannelService.selectById(costChannelId);
    }


}
